package com.ym.jiujiangeyou;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ym.jiujiangeyou.adapter.CollectListAdapter;
import com.ym.jiujiangeyou.app.AppManager;
import com.ym.jiujiangeyou.database.HotelService;
import com.ym.jiujiangeyou.entity.Guide;
import com.ym.jiujiangeyou.entity.Hotel;
import com.ym.jiujiangeyou.entity.Scenery;
import com.ym.jiujiangeyou.ui.DialogView;
import com.ym.jiujiangeyou.util.Constants;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 /**   
 * @author 叶铭   
 * @email yeming_1001@163.com
 * @version V1.0   
 */
@SuppressLint({ "HandlerLeak", "ResourceAsColor" })
public class HotelCollectList extends Activity {

	private int action;
	private TextView title_text;
	private ListView listView;
	private CollectListAdapter adapter;
	private HotelService service ;
	private List<Hotel> hotels,Hlist ;
	Dialog dialog;
	Toast toast;
	Handler handler = null ;
	ImageView collect_del_img ;
	boolean isDel = false ;   //是否删除标志
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);
		super.setContentView(R.layout.hotel_collect);

		Intent intent = getIntent();
		action = intent.getIntExtra("action", 0);

		init();

		if (Hlist.isEmpty()) {
			loadData(handler, action);
		}
	}
	
	public void init() {
		title_text = (TextView) findViewById(R.id.title_text);
		listView = (ListView) findViewById(R.id.hotel_collectlist);
		collect_del_img = (ImageView) findViewById(R.id.collect_del_img) ;

		dialog = DialogView.popDialog(this, R.string.isloading);
		hotels = new ArrayList<Hotel>() ;
		Hlist = new ArrayList<Hotel>() ;
	//	Nlist = new ArrayList<Hotel>() ;
		service = new HotelService(this) ;
		adapter = new CollectListAdapter(this,Constants.LISTVIEW_ACTION_GET_HOTEL);
		title_text.setText(R.string.title_text_collectlist);
		
		handler = new Handler(){
			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				Map<String, Object> map;
				switch (msg.what) {
				case Constants.LISTVIEW_ACTION_GET_HOTEL:
					map = (Map<String, Object>) msg.obj;
					hotels = (List<Hotel>) map.get("hotel") ;
					if(hotels.size() < 1){
						Toast.makeText(HotelCollectList.this, "没有收藏信息", Toast.LENGTH_SHORT).show() ;
					}
					Hlist.addAll(hotels);
					adapter.setHotelData(Hlist) ;
					listView.setAdapter(adapter);
					dialog.dismiss();
					break;
				case Constants.LISTVIEW_ACTION_GET_ERROR:
					dialog.dismiss();
					toast = DialogView.popToast(HotelCollectList.this,
							R.string.load_error);
					toast.show();
					break;
				case Constants.LIST_COLLECT_IS_DEL:
					isDel = true ;
					adapter.setIsDel(isDel) ;
					adapter.setHotelData(Hlist) ;
					listView.setAdapter(adapter);
					break ;
				case Constants.LIST_COLLECT_NO_DEL:
					isDel = false ;
					adapter.setIsDel(isDel) ;
					adapter.setHotelData(Hlist) ;
					listView.setAdapter(adapter);
					break ;
				case Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN:
					Hlist.clear() ;
					map = (Map<String, Object>) msg.obj;
					hotels = (List<Hotel>) map.get("hotel") ;
					if(hotels.size() < 1){
						Toast.makeText(HotelCollectList.this, "没有收藏信息", Toast.LENGTH_SHORT).show() ;
					}
					Hlist.addAll(hotels);
					adapter.setHotelData(Hlist) ;
					listView.setAdapter(adapter);
					dialog.dismiss();
					break ;
				case Constants.SCENERY_COLLECT_NOT:
					dialog.dismiss();
					Toast.makeText(HotelCollectList.this, "你还没有收藏酒店信息", Toast.LENGTH_SHORT).show() ;
					break ;
				default:
					break;
				}
				adapter.notifyDataSetChanged();
				
			}
		} ;
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Hotel hotel = Hlist.get(arg2) ;
				if(!isDel){
				Intent intent = new Intent(HotelCollectList.this,HotelDetails.class) ;
				Bundle bundle = new Bundle() ;
				bundle.putDouble("longitude", hotel.getLongitude()) ;
				bundle.putDouble("latitude", hotel.getLatitude()) ;
				bundle.putInt("id", hotel.getId()) ;
				bundle.putString("name", hotel.getName()) ;
				bundle.putString("address", hotel.getAddress()) ;
				bundle.putString("introduction", hotel.getIntroduction()) ;
				bundle.putString("phone", hotel.getPhone()) ;
				bundle.putString("facility", hotel.getFacility()) ;
				bundle.putString("imageurl", hotel.getImageurl()) ;
				bundle.putString("traffic", hotel.getTraffic()) ;
				bundle.putString("price", hotel.getPrice()) ;
				bundle.putString("priceinfo", hotel.getPriceinfo()) ;
				//intent.putExtra("hotel", hCountlv.get(position-1)) ;
				intent.putExtra("hotel", bundle) ;
				startActivity(intent) ;
				}else{
					HotelService hotelService = new HotelService(HotelCollectList.this) ;
					hotelService.delByName(hotel.getName()) ;
					loadData(handler,Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN ) ;
				}
			}
		}) ;
		
		collect_del_img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "del", Toast.LENGTH_SHORT).show() ;
				Message msg_del = new Message() ;
				if(!isDel){
					msg_del.what = Constants.LIST_COLLECT_IS_DEL ;
				}else{
					msg_del.what = Constants.LIST_COLLECT_NO_DEL ;
					
				}
				handler.sendMessage(msg_del) ;
			}
			
			
		}) ;
		
		/*listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				Hotel h = Hlist.get(arg2) ;
				arg1.setBackgroundColor(R.color.rosybrown) ;
				HotelService hotelService = new HotelService(HotelCollectList.this) ;
				hotelService.delByName(h.getName()) ;
				loadData(handler,Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN ) ;
				return false;
				
			}
		}) ;*/
	}
	
	/**
	 * @param handler
	 * @param action
	 * @retur void
	 * @Description: 加载数据库数据
	 */
	void loadData(final Handler handler, final int action) {
		dialog.show();
		new Thread(new Runnable() {
			Message message = new Message();
			Map<String, Object> map;

			@Override
			public void run() {
				// TODO Auto-generated method stub
				switch (action) {
				case Constants.LISTVIEW_ACTION_GET_HOTEL:
					
					try {
						map = service.query();
						if (!map.isEmpty()) {
							message.what = Constants.LISTVIEW_ACTION_GET_HOTEL;
							message.obj = map;
							Log.d("System.out", "------------------1"+map.size()) ;
							

						} else {
							message.what = Constants.SCENERY_COLLECT_NOT;
							Log.d("System.out", "------------------2") ;
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Log.d("System.out", "------------------3") ;
						message.what = Constants.LISTVIEW_ACTION_GET_ERROR;
					}
					
					break;
				case Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN:
					
					try {
						map = service.query();
						if (!map.isEmpty()) {
							message.what = Constants.LISTVIEW_ACTION_GET_HOTEL_AGIAN;
							message.obj = map;
							Log.d("System.out", "------------------1") ;
						} else {
							message.what = Constants.SCENERY_COLLECT_NOT;
							Log.d("System.out", "------------------2") ;
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Log.d("System.out", "------------------3") ;
						message.what = Constants.LISTVIEW_ACTION_GET_ERROR;
					}
					
					break;
				default:
					break;
				}
				handler.sendMessage(message);
			}
		}).start();
	}


}
